package com.example.teamwork1.service;


import com.example.teamwork1.mapper.t_userMapper;
import com.example.teamwork1.model.t_user;
import com.example.teamwork1.util.EmailUtil;
import com.example.teamwork1.util.JWTUtil;
import com.example.teamwork1.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import static com.example.teamwork1.util.Common.EncoderByMd5;

@Service
public class t_userService {

    @Autowired
    t_userMapper t_userMapper;

    public String loginCheck(String username,String password)
    {
        String str;
        if(username.length()<=5||password.length()<=5)
        {
            str ="账号密码太短啦";
        }
        else{
            t_user U= t_userMapper.findByUsername(username);
            //通过用户名查找用户
            if(U==null)
            {
                U=t_userMapper.findByMail(username);
                //若无法通过用户名查找，尝试通过邮箱查找用户
            }
            if(U==null)
            {
                str="账号或邮箱不存在";
            }
            else
            {
                if(U.getPassword().equals(EncoderByMd5(password)))
                    str = JWTUtil.getToken(U);
                else
                    str = "密码错误";
            }
        }
        return  str;
    }

    public String registerCheck(t_user U) {
        String  str;
        if(U.getGender().equals("1"))
            U.setGender("男");
        else
            U.setGender("女");
        if(U.getUsername().length()<=5||U.getPassword().length()<=5)
        {
            str ="账号密码太短啦!";
        }else if(U.getMail().indexOf('@')==-1){
            str ="邮箱格式不正确!";
        }else if(U.getPhone().length()<=10){
            str ="手机号太短啦!";
        }else if(t_userMapper.findByUsername(U.getUsername())!=null)
        {
            str="账号已存在，请尝试找回密码";
        }else if(t_userMapper.findByMail(U.getMail())!=null){
            str="邮箱已存在，请尝试通过邮箱登录";
        }else{
            try {
                t_userMapper.insert(U.getUsername(),U.getNickname(), EncoderByMd5(U.getPassword()), U.getPhone(), U.getMail(), U.getGender());
                str="注册成功";
            }catch (Exception e)
            {
                e.printStackTrace();
                str="注册遇到未知错误";
            }
        }

        return str;
    }

    public String  seekPasswordCheck(String username, String new_password ,String mail ,String CheckCode) {
        String  str;
        t_user u =t_userMapper.findByUsername(username);
        if(u==null)
        {
            str="账号不存在";
        }else if(!u.getMail().equals(mail)){
            str="邮箱不正确";
        }else if(!u.getCheckCode().equals(CheckCode)){
            str="验证码不正确";
        } else if (new_password.length()<6){
            str="密码长度过短";
        } else  {
            try {
                t_userMapper.updatePassword(username, EncoderByMd5(new_password));
                t_userMapper.updateCheckCodeByEMail(null,mail);//清空验证码
                str="改密成功";
            } catch (Exception e)
            {
                e.printStackTrace();
                str="未知错误";
            }
        }
        return str;
    }

    public String GetEMailCode(String EMail) {
        String checkCode=RandomUtil.getSixBitRandom();
        String message="你的验证码是:"+checkCode;
        boolean flag;
        try{
            flag=EmailUtil.sendEmail(EMail,"验证码邮件",message);
            t_userMapper.updateCheckCodeByEMail(checkCode,EMail);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        if(flag==true)
            return checkCode;
        else
            return "";
    }

    public String loginCheckByMail(String mail, String code) {
        String str;
        if(mail.indexOf('@')==-1) {
            str="邮箱格式不正确";
        } else {
            t_user u=t_userMapper.findByMail(mail);
            if(u==null){
                str="邮箱不存在";
            }else if(!u.getCheckCode().equals(code))
            {
                str="验证码不正确";
            }else{
                str=JWTUtil.getToken(u);
                t_userMapper.updateCheckCodeByEMail(null,mail);//清空验证码
            }
        }
        return str;
    }
}
